<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib uri="/WEB-INF/tld/security.tld" prefix="s" %>

<fmt:setBundle basename="messages"/>
<script type="text/javascript" language="javascript">
	function submitFutureQuery(form, orderField, orderType){
		form.orderField.value = orderField;
		form.orderType.value = orderType;
		//alert("form.orderField.value->"+form.orderField.value);
		//alert("form.orderType.value->"+form.orderType.value);
		form.submit();
	}
	function submitForm(form){
		form.submit();
	}
	function checkWithoutClient(){
		var withoutClient = document.getElementById('fpsView.withoutClient').checked;											
		if(withoutClient){					
			//document.getElementById('fpsView.withoutProduct').checked = true;
			document.getElementById('fpsView.clientDescription').disabled = true;
			//document.getElementById('fpsView.productDescription').disabled = true;
			document.getElementById('fpsView.clientDescription').value = "";
			//document.getElementById('fpsView.productDescription').value = "";			
		}else{
			document.getElementById('fpsView.clientDescription').disabled = false;
			//document.getElementById('fpsView.productDescription').disabled = false;
			//withoutProduct = document.getElementById('fpsView.withoutProduct').checked = false;
		}		
	}
	function checkWithoutProduct(){
		var withoutProduct = document.getElementById('fpsView.withoutProduct').checked;								
		if(withoutProduct){			
			document.getElementById('fpsView.productDescription').disabled = true;			
			document.getElementById('fpsView.productDescription').value = "";	
		}else{			
			document.getElementById('fpsView.productDescription').disabled = false;
			//document.getElementById('fpsView.clientDescription').disabled = false;
		}
	}
	
</script>
<html:form action="/searchFuturePurchaseQuery" method="POST">
<table width="100%">
	<tr>
		<td>

				<html:hidden property="orderField"/>
				<html:hidden property="orderType"/>
				<table align="center" width="40%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
					<tr>
						<td colspan="4" class="titulocolumna" align="center"><bean:message key="menu.module.corn.future"/></td>
					</tr>
					<tr>
						<td style="text-align: right;" class="etiqueta">
							<fmt:message key="label.futurepurchase.query.periodFrom"/>:
						</td>
						<td style="text-align: center;" class="etiqueta_campo">
							<select name="fpsView.beginPeriodYear" class="campo_texto">
								<c:forEach items="${periodYears}" var="year">
									<c:choose>
										<c:when test="${year == bPeriodYear}">
											<option value="<c:out value="${year}"/>" selected="selected"><c:out value="${year}"/></option>
										</c:when>
										<c:otherwise>
											<option value="<c:out value="${year}"/>"><c:out value="${year}"/></option>
										</c:otherwise>
									</c:choose>
								</c:forEach>
							</select>
						</td>
						<td style="text-align: right;"  class="etiqueta">
							<fmt:message key="label.futurepurchase.query.periodTo"/>:
						</td>
						<td style="text-align: center;" class="etiqueta_campo">
							<select name="fpsView.endPeriodYear" class="campo_texto">
								<c:forEach items="${periodYears}" var="year">
									<c:choose>
										<c:when test="${year == ePeriodYear}">
											<option value="<c:out value="${year}"/>" selected="selected"><c:out value="${year}"/></option>
										</c:when>
										<c:otherwise>
											<option value="<c:out value="${year}"/>"><c:out value="${year}"/></option>
										</c:otherwise>
									</c:choose>
								</c:forEach>
							</select>
						</td>
					
					</tr>
					
					<tr>
					 	<td style="text-align: right;" class="etiqueta">
							<fmt:message key="message.futurepurchase.customer"/>:		 
						</td>
						
						<td colspan="3" style="text-align: left;" class="etiqueta_campo">
						    &nbsp;&nbsp;<html:text style="width: 200 px;" name="SearchFuturePurchaseForm" property="fpsView.clientDescription"  styleId="fpsView.clientDescription" styleClass="campo_texto" />
						    <html:checkbox name="SearchFuturePurchaseForm" property="fpsView.withoutClient" onclick="checkWithoutClient();" /> <fmt:message key="message.futurepurchase.with.client"/>&nbsp;&nbsp;						    
						</td>																					
					</tr>
					
					
					<tr>
					 	<td style="text-align: right;" class="etiqueta">
							<fmt:message key="message.futurepurchase.product"/>:		 
						</td>
						
						<td colspan="3" style="text-align: left;" class="etiqueta_campo">
						   	&nbsp;&nbsp;<html:text style="width: 200 px;" name="SearchFuturePurchaseForm" property="fpsView.productDescription" styleId="fpsView.productDescription" styleClass="campo_texto" />
						    <html:checkbox name="SearchFuturePurchaseForm" property="fpsView.withoutProduct" onclick="checkWithoutProduct();" /> <fmt:message key="message.futurepurchase.without.poduct"/>						    
						</td>																					
					</tr>
					
					<tr>
					
					<td align="right" class=columnadatoPar colspan="4">
							<input type="submit" value="<fmt:message key="label.common.query"/>"  class="boton"/>
						</td>
					</tr>
																				
				</table>

		</td>
	</tr>
	<tr>
		<td>
			<div style="background-color: #C0C0C0; border: .1px solid;"/>
		</td>
	</tr>
	<tr>
		<td>
			<table align="center" cellpadding="1" cellspacing="1" border="0" bgcolor="#CCCCC0" width="98%">
				<tr>
					<td class="titulocolumna" align="center" width="4%">
						<s:entidad nombreCortoMod="PROC" nombreCortoSub="FUTURES" nombreCortoEnt="NEW" permiso="ADMINISTRACION">
							<html:link action="/futurePurchaseNew"><img alt="<fmt:message key="common.add"/> <fmt:message key="label.purchase.program.futures"/>" src="images/nuevo.gif" border="0"></html:link>
						</s:entidad>
					</td>
					<td class="titulocolumna" align="center" width="4%">&nbsp;</td>
					<td class="titulocolumna" align="center" width="8%">
						<a href="#>" onclick="return submitFutureQuery(document.SearchFuturePurchaseForm, 0,'<c:choose><c:when test="${previousOrderField == 0 && previousOrderType == 0}">1</c:when><c:otherwise>0</c:otherwise></c:choose>');" class="ligas">
							<fmt:message  key="message.futurepurchase.periodaccount"/>
							<c:choose>
								<c:when test="${previousOrderField == 0 && previousOrderType == 1}">
									<img alt="<bean:message key="label.common.order.ascending"/>" src="images/arrow_up.gif" border="0"/>
								</c:when>
								<c:when test="${previousOrderField == 0 && previousOrderType == 0}">
									<img alt="<bean:message key="label.common.order.descending"/>" src="images/arrow_down.gif" border="0"/>
								</c:when>
							</c:choose>
						</a>
					</td>
					
					<td class="titulocolumna" align="center"  width="8%">		
						<a href="#>" onclick="return submitFutureQuery(document.SearchFuturePurchaseForm, 1,'<c:choose><c:when test="${previousOrderField == 1 && previousOrderType == 0}">1</c:when><c:otherwise>0</c:otherwise></c:choose>');" class="ligas">
							<fmt:message  key="message.futurepurchase.periodapplication"/>
							<c:choose>
								<c:when test="${previousOrderField == 1 && previousOrderType == 1}">
									<img alt="<bean:message key="label.common.order.ascending"/>" src="images/arrow_up.gif" border="0"/>
								</c:when>
								<c:when test="${previousOrderField == 1 && previousOrderType == 0}">
									<img alt="<bean:message key="label.common.order.descending"/>" src="images/arrow_down.gif" border="0"/>
								</c:when>
							</c:choose>
						</a>
					</td>
					
					<td class="titulocolumna" align="center" width="7%">
						<a href="#>" onclick="return submitFutureQuery(document.SearchFuturePurchaseForm, 2,'<c:choose><c:when test="${previousOrderField == 2 && previousOrderType == 0}">1</c:when><c:otherwise>0</c:otherwise></c:choose>');" class="ligas">
							<fmt:message  key="label.futurepurchase.query.quantity"/>
							<c:choose>
								<c:when test="${previousOrderField == 2 && previousOrderType == 1}">
									<img alt="<bean:message key="label.common.order.ascending"/>" src="images/arrow_up.gif" border="0"/>
								</c:when>
								<c:when test="${previousOrderField == 2 && previousOrderType == 0}">
									<img alt="<bean:message key="label.common.order.descending"/>" src="images/arrow_down.gif" border="0"/>
								</c:when>
							</c:choose>
						</a>
					</td>
					<td class="titulocolumna" align="center" width="8%">
						<a href="#>" onclick="return submitFutureQuery(document.SearchFuturePurchaseForm, 3,'<c:choose><c:when test="${previousOrderField == 3 && previousOrderType == 0}">1</c:when><c:otherwise>0</c:otherwise></c:choose>');" class="ligas">
							<fmt:message  key="label.futurepurchase.query.unitPrice"/>
							<c:choose>
								<c:when test="${previousOrderField == 3 && previousOrderType == 1}">
									<img alt="<bean:message key="label.common.order.ascending"/>" src="images/arrow_up.gif" border="0"/>
								</c:when>
								<c:when test="${previousOrderField == 3 && previousOrderType == 0}">
									<img alt="<bean:message key="label.common.order.descending"/>" src="images/arrow_down.gif" border="0"/>
								</c:when>
							</c:choose>
						</a>
					</td>
					<td class="titulocolumna" align="center" width="7%">
						<a href="#>" onclick="return submitFutureQuery(document.SearchFuturePurchaseForm, 4,'<c:choose><c:when test="${previousOrderField == 4 && previousOrderType == 0}">1</c:when><c:otherwise>0</c:otherwise></c:choose>');" class="ligas">
							<fmt:message  key="label.futurepurchase.query.price"/>
							<c:choose>
								<c:when test="${previousOrderField == 4 && previousOrderType == 1}">
									<img alt="<bean:message key="label.common.order.ascending"/>" src="images/arrow_up.gif" border="0"/>
								</c:when>
								<c:when test="${previousOrderField == 4 && previousOrderType == 0}">
									<img alt="<bean:message key="label.common.order.descending"/>" src="images/arrow_down.gif" border="0"/>
								</c:when>
							</c:choose>
						</a>
					</td>
					<td class="titulocolumna" align="center" width="8%">
						<a href="#>" onclick="return submitFutureQuery(document.SearchFuturePurchaseForm, 5,'<c:choose><c:when test="${previousOrderField == 5 && previousOrderType == 0}">1</c:when><c:otherwise>0</c:otherwise></c:choose>');" class="ligas">
							<fmt:message  key="message.futurepurchase.cornorigin"/>
							<c:choose>
								<c:when test="${previousOrderField == 5 && previousOrderType == 1}">
									<img alt="<bean:message key="label.common.order.ascending"/>" src="images/arrow_up.gif" border="0"/>
								</c:when>
								<c:when test="${previousOrderField == 5 && previousOrderType == 0}">
									<img alt="<bean:message key="label.common.order.descending"/>" src="images/arrow_down.gif" border="0"/>
								</c:when>
							</c:choose>
						</a>
					</td>					
					<td class="titulocolumna" align="center" width="40%">
						<fmt:message key="label.futurepurchase.query.customerPurchases"/>
					</td>
					<td class="titulocolumna" align="center" width="4%">&nbsp;</td>
				</tr>
				<c:forEach items="${futurePurchases}" var="futurePurchase" varStatus="colum">
					<% 	java.util.HashMap mapa=new java.util.HashMap(0);%>
					<bean:define id="id" name="futurePurchase" property="futurePurchaseId"></bean:define>
					<% 	mapa.put("fpId",id);
						request.setAttribute("mapa",mapa); 
					%> 
					<tr class="<c:choose><c:when test="${colum.index%2 == 0}"><c:out value="columnadatoPar" /></c:when><c:otherwise><c:out value="columnadatoNon" /></c:otherwise></c:choose>">
						<td valign="top" align="center">
							<s:entidad nombreCortoMod="PROC" nombreCortoSub="FUTURES" nombreCortoEnt="EDIT" permiso="ADMINISTRACION">
								<html:link action="/futurePurchaseGoUpdate" name="mapa"><img alt="<fmt:message key="common.edit"/> <c:out value="${colum.index+1}"></c:out>" src="images/modificar.gif" border="0"></html:link>
							</s:entidad>
						</td>
						<td valign="top" align="center">
							<c:out value="${colum.index+1}"></c:out>
						</td>
						<td valign="top">
							<c:out value="${futurePurchase.accountFuturePeriodName}"/>
						</td>
						<td valign="top">
							<c:out value="${futurePurchase.applicationFuturePeriodName}"/>
						</td>
						<td valign="top" align="right">
							<fmt:formatNumber pattern="###,###,###" value="${futurePurchase.quantity}"/>
						</td>
						<td valign="top" align="right">
							<fmt:formatNumber maxFractionDigits="4" pattern="###,###,##0.0000" value="${futurePurchase.unitPrice}"/>
						</td>
						<td valign="top" align="right">
							<fmt:formatNumber maxFractionDigits="4" pattern="###,###,##0.0000" value="${futurePurchase.price}"/>
						</td>
						<td valign="top" align="center">
							<c:out value="${futurePurchase.cornOrigin}"/>
						</td>
						
						<td valign="top">
							<table width="100%">
								<c:set var="result" value="${futurePurchase.quantity}"></c:set>																						
								<c:forEach items="${futurePurchase.customerPurchases}" var="customerPurchase">
									<tr class="<c:choose><c:when test="${colum.index%2 == 0}"><c:out value="columnadatoPar" /></c:when><c:otherwise><c:out value="columnadatoNon" /></c:otherwise></c:choose>">
										<td align="left">
											&nbsp;<c:out value="${customerPurchase.customerId}"/> - <c:out value="${customerPurchase.description}"/> - [&nbsp;<c:out value="${customerPurchase.quantity}"/><c:out value="${customerPurchase.futureTypeId}"/>]																																																				
											<c:set var="result" value="${result - customerPurchase.quantity}" />																													
										</td>
									</tr>
								</c:forEach>
								<tr class="<c:choose><c:when test="${colum.index%2 == 0}"><c:out value="columnadatoPar" /></c:when><c:otherwise><c:out value="columnadatoNon" /></c:otherwise></c:choose>">
									<td align="left">										
										<logic:greaterThan name="result" value="0">
											<bean:message key="message.futurepurchase.advanced.purchase.single"/> -
											&nbsp;[&nbsp;<c:out value="${result}" />&nbsp;] 
										</logic:greaterThan>
									</td>
								</tr>
																				
							</table>
						</td>
						<td valign="top" align="center">
							<s:entidad nombreCortoMod="PROC" nombreCortoSub="FUTURES" nombreCortoEnt="DELETE" permiso="ADMINISTRACION">
								<a href="#>" onclick="if(confirmDelete('<c:out value="${colum.index+1}"></c:out>'))location.href='futurePurchaseDelete.do?fpId=<bean:write name="id"/>';">
									<img alt="<bean:message key="common.delete"/> <c:out value="${colum.index+1}"></c:out>" src="images/eliminar.jpg" border="0">
								</a>
							</s:entidad>
						</td>
					</tr>
				</c:forEach>
			</table>
		</td>
	</tr>
</table>
</html:form>
<script>	
	cleanNumber(document.getElementById('fpsView.clientDescription'));
	cleanNumber(document.getElementById('fpsView.productDescription'));
</script>